package com.qys.livesMall.financial.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.financial.model.dto.sys.*;
import com.qys.livesMall.financial.model.entity.FinancialOnline;
import com.qys.livesMall.financial.model.entity.FinancialOnlineDetail;
import com.qys.livesMall.financial.model.vo.FinancialOnlineRecordVO;
import com.qys.livesMall.financial.model.vo.FinancialOnlineVO;
import com.qys.livesMall.financial.model.vo.sys.FinancialOfflinePageVO;
import com.qys.livesMall.financial.model.vo.sys.FinancialOfflineVO;
import com.qys.livesMall.financial.model.vo.sys.FinancialOnlinePageVO;
import com.qys.livesMall.financial.service.FinancialOfflineService;
import com.qys.livesMall.financial.service.FinancialOnlineDetailService;
import com.qys.livesMall.financial.service.FinancialOnlineService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 线上理财表 前端控制器
 *
 * @author
 * @since 2023-06-13
 */
@RestController
@Tag(name = "后台管理-线上理财")
@RequestMapping("/sys/financial/online")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysFinancialOnlineController {

    private final FinancialOnlineService financialOnlineService;
    private final FinancialOnlineDetailService financialOnlineDetailService;


    @PostMapping("/page")
    @Operation(summary = "分页查询线上理财信息")
    public R<Page<FinancialOnlinePageVO>> page(@RequestBody FinancialOnlinePageDTO pageDTO) {
        return R.ok(financialOnlineService.pageBySys(pageDTO));
    }

    @PostMapping("/add")
    @Operation(summary = "新增线下理财信息")
    public R<Void> add(@RequestBody @Validated FinancialOnlineSaveDTO saveDTO) {
        financialOnlineService.saveOrUpdate(saveDTO);
        return R.ok();
    }

    @PutMapping("/edit")
    @Operation(summary = "编辑线上理财信息")
    public R<Void> edit(@RequestBody @Validated FinancialOnlineSaveDTO saveDTO) {
        financialOnlineService.saveOrUpdate(saveDTO);
        return R.ok();
    }
    @GetMapping("/getinfo/{id}")
    @Operation(
            summary = "线上理财详情",
            parameters = {
                    @Parameter(name = "id", description = "理财id")
            }
    )
    public R<FinancialOnlineVO> getinfo(@PathVariable Long id){
        return R.ok(financialOnlineService.getinfo(id));
    }

    @Operation(
            summary = "删除线上理财",
            parameters = {
                    @Parameter(name = "id", description = "理财id")
            }
    )
    @DeleteMapping(value = "/delete/{id}")
    public R<Void> deleteById(@PathVariable Long id) {
        financialOnlineService.deleteById(id);
        return R.ok();
    }


    @PutMapping("/shelves/{id}")
    @Operation(
            summary = "上架下架",
            parameters = {
                    @Parameter(name = "id", description = "理财id")
            }
    )
    public R<Void> shelves(@PathVariable("id") Long id) {
        financialOnlineService.shelves(id);
        return R.ok();
    }


    @Operation(summary = "线上理财购买记录")
    @PostMapping(value = "/buyRecords")
    public R<Page<FinancialOnlineRecordVO>> buyRecords(@RequestBody FinancialOnlineRecordPageDTO pageDTO) {
        return R.ok(financialOnlineDetailService.pageBySys(pageDTO));
    }

    @Operation(summary = "线上理财购买记录-查看合同")
    @GetMapping(value = "/getBuyContract/{id}")
    public R<String> getBuyContract(@PathVariable Long id) {
        return R.ok( financialOnlineDetailService.getBuyContract(id));
    }
}
